package com.jmazgaj.satel;

import java.io.ByteArrayOutputStream;

import com.jmazgaj.satel.utils.CRCUtils;

public abstract class SatelCommand<T> {

	protected abstract int getActionCode();
	protected abstract byte [] getRequestData();
	protected abstract int getResponseDataLength();
	protected abstract T processResponse(byte [] response);
	
	protected T response;
	
	public T getResponse() {
		return response;
	}
	
	
	byte [] createRequest() {
		int action = getActionCode();
		byte [] request = getRequestData();
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		out.write(0xFE);
		out.write(0xFE);
		
		int crc = CRCUtils.calculateCRC(action, request);
		out.write(action);
		
		for(byte b : request) {
			out.write(b);
		}
		
		out.write(crc >> 8);
		out.write(crc & 0xFF);
		
		out.write(0xFE);
		out.write(0X0D);
		
		return out.toByteArray();
	}
	
	void setResponse(byte [] response) {
		this.response = processResponse(response);
	}

}
